import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useLoadingStore = defineStore('loading', () => {
  // 全局加载状态
  const isLoading = ref(false)
  // 加载提示文本
  const loadingText = ref('')
  // 加载计数器（处理多个并发请求）
  const loadingCounter = ref(0)

  // 开始加载
  const startLoading = (text = '加载中...') => {
    loadingCounter.value++
    isLoading.value = true
    loadingText.value = text
  }

  // 结束加载
  const endLoading = () => {
    loadingCounter.value--
    if (loadingCounter.value <= 0) {
      loadingCounter.value = 0
      isLoading.value = false
      loadingText.value = ''
    }
  }

  // 重置加载状态
  const resetLoading = () => {
    loadingCounter.value = 0
    isLoading.value = false
    loadingText.value = ''
  }

  return {
    isLoading,
    loadingText,
    startLoading,
    endLoading,
    resetLoading
  }
}) 